package com.avic.gateway.filter;

import com.avic.gateway.common.constant.GatewayConstant;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;

/**
 * 登出删除Token
 *
 * @author: huangsheng
 * @date: 2018/8/24
 */
@Slf4j
public class LogOutRemoveTokenPostFilter extends AbstractZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    @Override
    public int filterOrder() {
        return 9;
    }

    @Override
    public boolean shouldFilter() {
        return shouldFilter(LogOutRemoveTokenPostFilter.class.getSimpleName());
    }

    @Override
    public Object run() {

        RequestContext requestContext = RequestContext.getCurrentContext();
        String userId = requestContext.getZuulRequestHeaders().get(GatewayConstant.HEADER_CURRENT_USER_ID_KEY);
        log.info("删除用户 {} 的Token信息", userId);
        if (StringUtils.isNotBlank(userId)) {
            redisTemplate.delete(GatewayConstant.REDIS_MANAGEMENT_TOKEN_SPACE + userId);
        }
        return null;
    }

}
